//
//  ChartViewController.m
//  edu
//
//  Created by baobaodp on 13-9-24.
//  Copyright (c) 2013年 baobaodp. All rights reserved.
//

#import "ChartViewController.h"

@interface ChartViewController ()

@end

@implementation ChartViewController

@synthesize chart;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    [self loadDocument:@"html/line-charts/notes.html"];
}

//加载本地使用说明文件文件
-(void)loadDocument:(NSString *)docName
{

    NSString *path = [[NSBundle mainBundle] pathForResource:@"notes" ofType:@"html"];
    [self.chartView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath: path]]];
    
//    NSURL *url=[NSURL fileURLWithPath:path];
//    NSURLRequest *request=[NSURLRequest requestWithURL:url];
//    self.chartView.scalesPageToFit=YES;
//    [self.chartView loadRequest:request];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark –
#pragma mark UIWebViewDelegate
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    if ( [request.mainDocumentURL.relativePath isEqualToString:@"/click/false"] ) {
        NSLog( @"not clicked" );
        return false;
    }
    if ( [request.mainDocumentURL.relativePath isEqualToString:@"/click/true"] ) {        //the image is clicked, variable click is true
        NSLog( @"image clicked" );
        UIAlertView* alert=[[UIAlertView alloc]initWithTitle:@"JavaScript called"
                                                     message:@"You’ve called iPhone provided control from javascript!!" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil];
        [alert show];
        return false;
    }
    return true;
}
- (void)webViewDidStartLoad:(UIWebView *)webView
{
    NSString *title = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];
    NSLog(@"title11=%@",title);
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    NSString *title = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];
    NSLog(@"title=%@",title);
    
    //添加数据
    [self.chartView stringByEvaluatingJavaScriptFromString: [chart getDataSource]];
    
    //createChartScript
    [self.chartView stringByEvaluatingJavaScriptFromString: [chart createChartScript]];
    
    
    
    [self.chartView stringByEvaluatingJavaScriptFromString: [self getDoScript]];
    
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
}

-(NSString *) getDoScript
{
    NSString * script = [NSString stringWithFormat:@" $(document).ready(function(e){ createChart(); %@});" ,[chart customizeScript]];
    return script;
}

@end
